package com.zh.note.huawei.twohundred.找出两个整数数组中同时出现的整数;

import java.util.*;

/**
 * @Author: zhanghuan
 * @date: 2023/8/23 11:19
 * @description: create it
 * <p>
 * 5,8,11,3,6,8,8,-1,11,2,11,11
 * 11,2,11,8,6,8,8,-1,8,15,3,-9,11
 */
public class Main {
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // String input1 = sc.nextLine();
        // String input2 = sc.nextLine();
        String input1 = "5,8,11,3,6,8,8,-1,11,2,11,11";
        String input2 = "11,2,11,8,6,8,8,-1,8,15,3,-9,11";
        String[] value1 = input1.split(",");
        String[] value2 = input2.split(",");

        Map<Integer, Integer> map1 = new HashMap<>();
        Map<Integer, Integer> map2 = new HashMap<>();
        // 1、统计第一行的元素出现次数
        for (String s : value1) {
            int num = Integer.parseInt(s);
            map1.put(num, map1.getOrDefault(num, 0) + 1);
        }
        // 2、统计第一行的元素出现次数
        for (String s : value2) {
            int num = Integer.parseInt(s);
            map2.put(num, map2.getOrDefault(num, 0) + 1);
        }
        //3、统计相同元素，出现次数
        // key是出现的次数，value是有哪些元素
        Map<Integer, List<Integer>> sameMap = new HashMap<>();
        boolean flag = true;
        for (Map.Entry<Integer, Integer> entry : map1.entrySet()) {
            int key = entry.getKey();
            int count = entry.getValue();
            if (map2.containsKey(key)) {
                flag = false;//表示找到了
                // 出现次数相同
                int sameCount = Math.min(count, map2.get(key));
                sameMap.computeIfAbsent(sameCount, integer -> new ArrayList<>()).add(key);
            }
        }
        //4、输出结果
        if (flag) {
            System.out.println("NULL");
        } else {
            List<Integer> res = new ArrayList<>(sameMap.keySet());
            Collections.sort(res);
            for (int count : res) {
                List<Integer> numbers = sameMap.get(count);
                Collections.sort(numbers);
                String[] sts = numbers.stream().map(String::valueOf).toArray(String[]::new);
                String join = String.join(",", sts);
                // String collect = numbers.stream().collect(Collectors.joining(","));
                System.out.println(count + ":" + join);
            }
        }


    }
}
